package light.mvc.service.hrs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.hrs.Thrsexpert;
import light.mvc.model.hrs.Thrsjudgement;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.hrs.HRSExpert;
import light.mvc.pageModel.hrs.HRSJudgement;
import light.mvc.service.hrs.HRSExpertServiceI;

@Service
public class HRSExpertServiceImpl implements HRSExpertServiceI {

	@Autowired
	private BaseDaoI<Thrsexpert> expertDao;
	@Autowired
	private BaseDaoI<Thrsjudgement> judgementDao;

	@Override
	public boolean add(HRSExpert expert) {
		Thrsexpert t = new Thrsexpert();

		t.setName(expert.getName());
		t.setPassword(expert.getPassword());
		t.setTel(expert.getTel());
		t.setDepartment(expert.getDepartment());

		expertDao.save(t);

		return false;
	}

	@Override
	public boolean login(String tel, String password) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tel", tel);
		params.put("password", password);
		Thrsexpert t = expertDao.get("from Thrsexpert t where t.tel= :tel and t.password = :password", params);
		if (t != null)
			return true;
		return false;
	}

	@Override
	public HRSExpert login(HRSExpert expert) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tel", expert.getTel());
		params.put("password", expert.getPassword());
		Thrsexpert t = expertDao.get("from Thrsexpert t where t.tel=:tel and t.password=:password", params);
		if (t != null) {
			HRSExpert u = new HRSExpert();
			BeanUtils.copyProperties(t, u);
			return u;
		}
		return null;

	}

	@Override
	public HRSExpert getByLoginName(HRSExpert expert) {
		Thrsexpert t = expertDao.get("from Thrsexpert t where t.tel= '" + expert.getTel() + "'");
		HRSExpert u = new HRSExpert();
		if (t != null) {
			BeanUtils.copyProperties(t, u);
		} else {
			return null;
		}
		return u;
	}

	@Override
	public boolean saveJudgement(HRSJudgement judgement) {
		Thrsjudgement t = new Thrsjudgement();
		// t.setId(judgement.getId());
		t.setPicGroupId(judgement.getPicGroupId());
		t.setExpertId(judgement.getExpertId());
		t.setMainType(judgement.getMainType());
		t.setSecondaryType(judgement.getSecondaryType());
		t.setLesserType(judgement.getLesserType());

		if (t == null) {
			System.out.println("t is null");
			return false;
		}
		judgementDao.save(t);

		return false;
	}

	@Override
	public List<HRSExpert> dataGrid(HRSExpert expert, PageFilter ph) {
		List<HRSExpert> ul = new ArrayList<HRSExpert>();
		Map<String, Object> params = new HashMap<String, Object>();

		String hql = "from Thrsexpert t ";
		String hql2 = hql + whereHql(expert, params) + orderHql(ph);

		List<Thrsexpert> l = expertDao.find(hql + whereHql(expert, params) + orderHql(ph), params, ph.getPage(),
				ph.getRows());

		if (l.isEmpty()) {
			System.out.println("专家数据为空");
		}
		for (Thrsexpert t : l) {
			HRSExpert u = new HRSExpert();
			BeanUtils.copyProperties(t, u);
			ul.add(u);
		}
		return ul;
	}

	@Override
	public Long count(HRSExpert expert, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Thrsexpert t ";
		return expertDao.count("select count(*) " + hql + whereHql(expert, params), params);
	}

	@Override
	public void delete(Long id, HttpServletRequest request) {
		Thrsexpert t = expertDao.get(Thrsexpert.class, id);
		expertDao.delete(t);
	}

	private String whereHql(HRSExpert expert, Map<String, Object> params) {
		String hql = "";
		if (expert != null) {
			hql += " where 1=1 ";

			/*
			 * 里面可以加上细腻的判断 if(expert.xxxx){
			 * 
			 * }
			 */
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	@Override
	public HRSExpert get(Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		Thrsexpert t = expertDao.get("from Thrsexpert t where t.id = :id", params);
		HRSExpert u = new HRSExpert();
		BeanUtils.copyProperties(t, u);
		return u;
	}

}
